/*
 * @Author: your name
 * @Date: 2020-11-30 19:46:53
 * @LastEditTime: 2020-12-18 12:12:02
 * @LastEditors: Please set LastEditors
 * @Description: In User Settings Edit
 * @FilePath: \H5 2010 studyd:\H5 2010 练习\考试\第二阶段 JQ\吴俊翰 steam\js\login\login.js
 */

require(['../config'],()=>{
    require(['header','footer','nav','jquery','cookie'],()=>{
        class Login{
            constructor(){
               this.registerBtnClick()
               this.userlogin()

            }
            registerBtnClick(){
               $('#registerBtn').click(function(){
                   window.location.href='/html/register/register.html'
               })
            }
            userlogin(){
                

                $('#loginBtn').click(function(){
                    $('.error').css({display:'none'})
                    const username = $('#username').val(),
                    password = $('#password').val()

                    const userlist = JSON.parse(localStorage.getItem('userlist'))

                    // 如果 userlist 有数据 就去判断 localStorage 里面的数据和自己输入的符不符合
                    const hasData = userlist.some(item => {
                        return item.username === username && item.password === password
                    })

                    if(hasData){

                        // 保存 cookie 
                        const str = JSON.stringify({username,password})
                        // setCookie('userInfo',str,{
                        //     path:'/'
                        // })
                        $.cookie('userInfo',str,{
                            expires: 7,
                            path:'/'
                        })

                        // alert('登陆成功')
                        // window.open('/index.html','_self')
                        
                        $('#login-success').css({display:'flex'})
                        $('#loginjumpBtn').click(function(){
                            window.open('/index.html', '_self')
                        })
                    }else{
                        const hasUsername = userlist.some(item => {
                            return item.username === username 
                        })
                        if(hasUsername){
                            $('.error').slideDown(300)
                        }else{
                            $('#login-undefined').css({display:'flex'})
                        $('#undefinedjumpBtn').click(function(){
                            window.open('/html/register/register.html', '_self')
                        })
                        }
                        
                    }

                })

            }

        }
        return new Login
    })
})
 
 // 跳转到注册页面
// $('#registerBtn').click(function(){
//     console.log(123)
//     window.location.href='/html/register/register.html'
// })

// 请求登录


// 判断账号是否正确


// 点击登录按钮的 loading 特效